#!/bin/bash
# 测试定时同步服务

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo -e "${GREEN}=== 测试 AKShare 期权数据定时同步服务 ===${NC}"
echo ""

cd "$PROJECT_ROOT"

# 1. 测试配置文件解析
echo -e "${YELLOW}1. 测试配置文件解析...${NC}"
if [ -f "config/sync_config.toml" ]; then
    echo "✓ 配置文件存在"
else
    echo "✗ 配置文件不存在"
    exit 1
fi

# 2. 编译项目
echo ""
echo -e "${YELLOW}2. 编译项目...${NC}"
cargo build -p akoption-scheduler 2>&1 | tail -n 5

if [ ${PIPESTATUS[0]} -eq 0 ]; then
    echo "✓ 编译成功"
else
    echo "✗ 编译失败"
    exit 1
fi

# 3. 运行单元测试
echo ""
echo -e "${YELLOW}3. 运行单元测试...${NC}"
cargo test -p akoption-scheduler --lib 2>&1 | grep -E "(test result|running)"

# 4. 测试一次性同步（模拟运行）
echo ""
echo -e "${YELLOW}4. 测试命令行参数解析...${NC}"
cargo run -p akoption-scheduler -- --help 2>&1 | head -n 10

echo ""
echo -e "${GREEN}=== 测试完成 ===${NC}"
echo ""
echo "下一步:"
echo "  1. 执行一次性同步测试: ./scripts/start_scheduler.sh --run-once"
echo "  2. 启动定时服务: ./scripts/start_scheduler.sh"
echo "  3. 安装为系统服务: sudo ./scripts/install_service.sh"
